def build_hashmap(file_d):
    """构建文件名到URL的映射字典"""
    hashmap = {}
    with open(file_d, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            parts = line.split('\t')
            if len(parts) == 2:
                filename, url = parts
                hashmap[filename] = url
            else:
                print(f"格式错误行: {line}")
    return hashmap


def match_files(file_c, hashmap, output_file):
    """匹配文件并仅输出URL"""
    with open(file_c, 'r', encoding='utf-8') as f:
        filenames = [line.strip() for line in f if line.strip()]

    matched_urls = []
    for filename in filenames:
        if filename in hashmap:
            matched_urls.append(hashmap[filename])

    with open(output_file, 'w', encoding='utf-8') as f:
        f.write('\n'.join(matched_urls))


if __name__ == '__main__':
    # 读取文件d构建hashmap,key：文件名 value：URL
    file_d = 'oss_batchsnap_list.txt'  # 存放 文件名\tURL 的文件
    file_c = 'c.txt'  # 存放待匹配文件名的文件
    output_file = 'e.txt'  # 输出结果文件
    hashmap = build_hashmap(file_d)
    match_files(file_c, hashmap, output_file)
    print(f"匹配完成，URL已保存到 {output_file}")

